<?php
namespace App\Http\Controllers\HongBao;

use App\Http\Controllers\Controller;
use App\Jobs\MLMRelateJob;
use App\Libraries\Decorator\Tool\IsEndDecorator;
use App\Libraries\Tools\ConstVar;
use App\Models\Config\Config;
use App\Models\HongBao\HongBao;
use App\Models\HongBao\HongBaoTemplate;
use App\Models\Shop\Shop;
use App\Models\User\User;
use Dingo\Api\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;


class HongBaoController extends Controller{

    protected $user;
    protected $hong_bao;
    protected $hong_bao_template;
    protected $is_end_decorate;
    public function __construct(User $user,
                                HongBao $hong_bao,
                                HongBaoTemplate $hong_bao_template,
                                IsEndDecorator $is_end){
        $this->user = $user;
        $this->hong_bao = $hong_bao;
        $this->hong_bao_template = $hong_bao_template;
        $this->is_end_decorate = $is_end;
    }

    /**
     * 领取红包
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $validator = Validator::make(
            $request->all(), [
            'mobile' => ['regex:/^1[34578][0-9]{9}$/', 'required'],
            'type' => 'required|in:1,2,3',//1-免费看信息 2-现金红包 3-积分红包
            'origin' => 'required',//红包来源
        ], [
            'mobile.required' => '10001',
            'mobile.regex' => '10006',
            'type.required' => '10001',
            'type.in' => '10002',
            'origin.required' => '10001',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $user_data = $this->user->getUserObjectByOpenIdOrMobile($request['mobile']);
        if(empty($user_data)){
            $this->user->mobile = $request['mobile'];
            $this->user->save();
            $user_data = $this->user;
        }
        $code = $this->hong_bao->addConfigHongBao(
            'receive_hong_bao_template',
            $user_data['user_id'],
            $request['origin']
        );
        if($code != 200){
            return $this->sendFailResponse(500, $code);
        }
        $origin = explode('_', $request['origin']);
        dispatch(new MLMRelateJob($origin[0], $user_data['user_id']));
        return $this->sendSuccessResponse($this->hong_bao);
    }

    /**
     * 红包列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function HongBaos(Request $request){
        $validator = Validator::make(
            $request->all(), [
            'status' => 'required|in:1,2',//1-当前可用 2-历史数据
            'type' => 'required|in:0,1,2,3',//0-全部 1-帖子红包 2-现金红包 3-积分红包
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'type.required' => '10001',
            'type.in' => '10002',
            'status.required' => '10001',
            'status.in' => '10002',
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $user_id = Auth::id();
        $this->hong_bao->addDecorator($this->is_end_decorate);
        $data = $this->hong_bao->getHongBaos(
            $request['page'],
            $request['size'],
            $request['type'],
            $request['status'],
            $user_id
        );
        return $this->sendSuccessResponse($data);
    }

    /**
     * 商家红包
     * @param $id
     * @param HongBaoTemplate $hongBaoTemplate
     * @param HongBao $hongBao
     * @param Shop $shop
     * @return \Illuminate\Http\JsonResponse
     */
    public function ShopHB($id, HongBaoTemplate $hongBaoTemplate, HongBao $hongBao, Shop $shop){
        $shopHBT = $hongBaoTemplate->getShopHBT($id);
        $shop = $shop->where('shop_id', $id)->where('status', 1)->first();
        $time = time();
        if(empty($shop)){
            return $this->sendFailResponse(400, 40400);
        }
        foreach($shopHBT as $k => $v){
            $hongBao->addTemplateHongBao(Auth::id(), 'shopHB', $v);
        }

        $data = $hongBao->withOnly('shop', ['shop_id', 'title'])->where('shop_id', $id)->where('user_id', Auth::id())
            ->where('type', ConstVar::$HONGBAO_TYPE_CASH)->where('status', 0)
            ->where('created_at', '>=', $time)
            ->get();
        return $this->sendSuccessResponse($data);
    }
}